package com.wgts.wgts.mvp.view.activity.goodsinfo;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class GoodsAllInfoFragment extends LazyBaseFragment {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private LinearLayoutManager linearLayoutManager;

    private GoodsAllInfoFragmentToActivityCallBack callBack;

    private ArrayList<Integer> arrayList = new ArrayList<>();
    GoodsAllInfoAdapter goodsAllInfoAdapter;


    private boolean isrefresh = false;

    private int headHeight;

    private boolean isPostion = false;

    @Override
    protected void loadData() {
        goodsAllInfoAdapter = new GoodsAllInfoAdapter(arrayList, "", getContext());
        recycleView.setAdapter(goodsAllInfoAdapter);
        //headHeight = recycleView.getLayoutManager().getChildAt(0).getHeight();
    }

    @Override
    public void init() {


    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.goods_all_info_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView(){
        initRefresh();
        initRecycle();
    }

    private void initRefresh(){
        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                goodsAllInfoAdapter.notifyDataSetChanged();
                //recycleView.smoothScrollToPosition(1);
                //callBack.gotodetail();
                refreshlayout.finishLoadmore();

                isrefresh = true;
                isPostion = false;
            }
        });
    }

    private void initRecycle(){
        linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int position = linearLayoutManager.findFirstVisibleItemPosition();
                View firstVisiableChildView = linearLayoutManager.findViewByPosition(position);
                int itemHeight = firstVisiableChildView.getHeight();
                Log.d("itemHeight", (position) * itemHeight - firstVisiableChildView.getTop() + "");
                int laspostion = linearLayoutManager.findLastVisibleItemPosition();






            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d("", dy + "");

                /*int position = linearLayoutManager.findFirstVisibleItemPosition();
                View firstVisiableChildView = linearLayoutManager.findViewByPosition(position);
                int itemHeight = firstVisiableChildView.getHeight();
                Log.d("itemHeight", (position) * itemHeight - firstVisiableChildView.getTop() + "");*/

                int position = linearLayoutManager.findFirstVisibleItemPosition();
                View firstVisiableChildView = linearLayoutManager.findViewByPosition(position);
                int itemHeight = firstVisiableChildView.getHeight();
                Log.d("itemHeight", (position) * itemHeight - firstVisiableChildView.getTop() + "");
                int laspostion = linearLayoutManager.findLastVisibleItemPosition();



                if(dy != 0){
                    if(dy > 0){
                        if(laspostion == 1){
                            if(isrefresh){
                                recycleView.smoothScrollToPosition(1);
                                callBack.recycleViewScroll( true, dy);
                            }
                        }
                    }else {
                        if(position == 0){
                            recycleView.smoothScrollToPosition(0);
                            callBack.recycleViewScroll( false, dy);
                        }
                    }
                }


            }
        });
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        callBack = (GoodsAllInfoFragmentToActivityCallBack)context;
    }



}
